package cd.safe.school.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import cd.safe.school.R;
import cd.safe.school.adapter.MusicAdapter;
import cd.safe.school.bean.MusicCache;

public class SelectMusicActivity extends Activity implements OnClickListener {
	private ListView music;
	private MusicAdapter ma;
	private List<MusicCache> list = new ArrayList<MusicCache>();
	private Button music_butn;
	private Button music_qx;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music);

		music = (ListView) findViewById(R.id.music);
		music_butn = (Button) findViewById(R.id.music_butn);
		music_qx = (Button) findViewById(R.id.music_qx);
		music_butn.setOnClickListener(this);
		music_qx.setOnClickListener(this);

		Cursor cursor = this.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				null);
		while (cursor.moveToNext()) {

			String title = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
			
			String path = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
			MusicCache mc = new MusicCache(title);
			mc.setPath(path);
			list.add(mc);

		}
		cursor.close();

		ma = new MusicAdapter(this, list);
		music.setAdapter(ma);
		music.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				boolean a = MusicAdapter.list.get(position).isSelect();
				if (a) {
					// MusicAdapter.list.get(position).setSelect(false);
				} else {
					for (MusicCache m : MusicAdapter.list) {
						m.setSelect(false);
					}
					MusicAdapter.list.get(position).setSelect(true);
				}
				ma.notifyDataSetChanged();

			}
		});
		music.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				music.requestFocusFromTouch();

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				music.requestFocusFromTouch();

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.music_butn:
			String path = "";
			String name = "";
			for (int i = 0; i < MusicAdapter.list.size(); i++) {
				MusicCache md = MusicAdapter.list.get(i);
				if (md.isSelect()) {
					path = md.getPath();
					name = md.getName();
				}
			}
			Intent intent = new Intent(SelectMusicActivity.this,
					EnglishEditDetailActivity.class);
			intent.putExtra("name", name);
			intent.putExtra("path", path);
			setResult(RESULT_OK, intent);
			finish();
			break;
		case R.id.music_qx:
			finish();
			break;

		}

	}
}
